<template>
  <div :style="!visible ? 'width: 0' : ''">
    <div v-show="visible" class="container">
      <div class="title">
        <b>{{loadSchool ? '学校' : '省市区' }}列表</b>
        <span title="收起列表" class="el-icon-d-arrow-left click-btn left" @click="visible = false"></span>
      </div>
      <el-tree
          :props="{children: 'children', label: 'label', isLeaf: 'leaf'}"
          @node-click="handleNodeClick"
          accordion
          node-key="value"
          :default-expanded-keys="activeList"
          :load="loadNode"
          lazy
          class="tree"
      ></el-tree>
    </div>
    <span v-show="!visible" title="收起列表" class="el-icon-d-arrow-right click-btn right"
          @click="visible = true"></span>
  </div>
</template>
<script>
import regions from "ele-admin/packages/regions"

export default {
  data() {
    return {
      visible: true
    };
  },
  props: {
    activeList: {
      type: Array,
      default: () => {
        return []
      }
    },
    loadSchool: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    visible(val) {
      this.$emit('visible', val)
    }
  },
  methods: {
    handleNodeClick(data) {
      if (data.leaf) {
        this.$emit('click', data)
      }
    },
    loadNode(node, resolve) {
      if (node.level === 0) {
        return resolve(regions.cityData)
      } else if (node.level === 1 || node.level === 2) {
        return resolve(node.data.children)
      } else {
        const getParentInfo = function (type, node, val) {
          const data = [node.data[type], val]
          let newNode = node.parent
          while (newNode.level > 0) {
            data.unshift(newNode.data[type])
            newNode = newNode.parent
          }
          return data
        }
        if (node.level === 3) {
          this.$http.get('/user/getStreetsList', {
            params: {district_code: node.data.value}
          }).then((res) => {
            resolve(
                res.data.data.map(item => {
                  return {
                    label: item.ext_name,
                    value: item.streets_code,
                    leaf: !this.loadSchool,
                    values: getParentInfo('value', node, item.streets_code),
                    labels: getParentInfo('label', node, item.ext_name)
                  }
                })
            );
          }).catch((e) => {
            this.$message.error(e.message);
          });
        } else if (node.level === 4) {
          this.$http.get('/schoole/index', {
            params: {
              province_code: node.parent.parent.parent.data.value,
              city_code: node.parent.parent.data.value,
              district_code: node.parent.data.value,
              streets_code: node.data.value,
              page: 1,
              limit: 9999,
            },
          }).then((res) => {
            resolve(
                res.data.data.map(item => {
                  return {
                    label: item.title,
                    value: item.id,
                    leaf: true,
                    location: item.location,
                    values: getParentInfo('value', node, item.id),
                    labels: getParentInfo('label', node, item.title)
                  };
                })
            );
          }).catch((e) => {
            this.$message.error(e.message)
          })
        }
      }
    }
  }
}
</script>

<style scoped>
.container {
  width: 300px;
  height: calc(100vh - 168px);
  border-radius: 4px;
  background: #fff;
  padding: 15px;
  margin-right: 15px;

  .title {
    display: flex;
    width: 100%;
    justify-content: space-between;
    position: relative;
  }

  .tree {
    height: calc(100vh - 220px);
    overflow-y: auto;
    margin-top: 20px;
  }
}

.click-btn {
  display: inline-block;
  text-align: center;
  line-height: 30px;
  background-color: #1890FF;
  width: 30px;
  color: #fff;
  cursor: pointer;

  &.left {
    border-top-left-radius: 50%;
    border-bottom-left-radius: 50%;
    position: absolute;
    right: -15px;
    top: 0;
  }

  &.right {
    border-top-right-radius: 50%;
    border-bottom-right-radius: 50%;
    position: relative;
    top: 15px;
    z-index: 9999;
  }
}


</style>